/// 工作台数据模型
class WorkbenchModel {
  /// 工作台项目列表
  List<WorkbenchItemModel>? items;
  
  /// 今日患者数量
  int todayPatientsCount;
  
  /// 今日处方数量
  int todayPrescriptionsCount;
  
  /// 待审核数量
  int pendingReviewsCount;
  
  /// 月收入
  int monthlyIncome;
  
  /// 是否有未读通知
  bool hasUnreadNotification;
  
  /// 构造函数
  WorkbenchModel({
    this.items,
    this.todayPatientsCount = 0,
    this.todayPrescriptionsCount = 0,
    this.pendingReviewsCount = 0,
    this.monthlyIncome = 0,
    this.hasUnreadNotification = false,
  });
  
  /// 从JSON创建对象
  factory WorkbenchModel.fromJson(Map<String, dynamic> json) {
    return WorkbenchModel(
      items: json['items'] != null 
          ? (json['items'] as List).map((item) => WorkbenchItemModel.fromJson(item)).toList()
          : null,
      todayPatientsCount: json['todayPatientsCount'] ?? 0,
      todayPrescriptionsCount: json['todayPrescriptionsCount'] ?? 0,
      pendingReviewsCount: json['pendingReviewsCount'] ?? 0,
      monthlyIncome: json['monthlyIncome'] ?? 0,
      hasUnreadNotification: json['hasUnreadNotification'] ?? false,
    );
  }
  
  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'items': items?.map((item) => item.toJson()).toList(),
      'todayPatientsCount': todayPatientsCount,
      'todayPrescriptionsCount': todayPrescriptionsCount,
      'pendingReviewsCount': pendingReviewsCount,
      'monthlyIncome': monthlyIncome,
      'hasUnreadNotification': hasUnreadNotification,
    };
  }
}

/// 工作台项目数据模型
class WorkbenchItemModel {
  /// 项目ID
  String? id;
  
  /// 项目名称
  String? title;
  
  /// 项目图标
  String? icon;
  
  /// 项目描述
  String? description;
  
  /// 项目类型
  String? type;
  
  /// 是否启用
  bool? enabled;
  
  /// 构造函数
  WorkbenchItemModel({
    this.id,
    this.title,
    this.icon,
    this.description,
    this.type,
    this.enabled,
  });
  
  /// 从JSON创建对象
  factory WorkbenchItemModel.fromJson(Map<String, dynamic> json) {
    return WorkbenchItemModel(
      id: json['id']?.toString(),
      title: json['title']?.toString(),
      icon: json['icon']?.toString(),
      description: json['description']?.toString(),
      type: json['type']?.toString(),
      enabled: json['enabled'] == true || json['enabled'] == 1,
    );
  }
  
  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'title': title,
      'icon': icon,
      'description': description,
      'type': type,
      'enabled': enabled,
    };
  }
}

/// 患者信息数据模型
class PatientModel {
  /// 患者ID
  String? patientId;
  
  /// 患者姓名
  String? name;
  
  /// 患者年龄
  int? age;
  
  /// 患者性别 1:男 2:女
  int? gender;
  
  /// 患者手机号
  String? phone;
  
  /// 患者身份证号
  String? idCard;
  
  /// 患者地址
  String? address;
  
  /// 病历号
  String? medicalRecordNumber;
  
  /// 构造函数
  PatientModel({
    this.patientId,
    this.name,
    this.age,
    this.gender,
    this.phone,
    this.idCard,
    this.address,
    this.medicalRecordNumber,
  });
  
  /// 从JSON创建对象
  factory PatientModel.fromJson(Map<String, dynamic> json) {
    return PatientModel(
      patientId: json['patient_id']?.toString(),
      name: json['name']?.toString(),
      age: json['age'] is int ? json['age'] : int.tryParse(json['age']?.toString() ?? ''),
      gender: json['gender'] is int ? json['gender'] : int.tryParse(json['gender']?.toString() ?? ''),
      phone: json['phone']?.toString(),
      idCard: json['id_card']?.toString(),
      address: json['address']?.toString(),
      medicalRecordNumber: json['medical_record_number']?.toString(),
    );
  }
  
  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'patient_id': patientId,
      'name': name,
      'age': age,
      'gender': gender,
      'phone': phone,
      'id_card': idCard,
      'address': address,
      'medical_record_number': medicalRecordNumber,
    };
  }
}

/// 处方信息数据模型
class PrescriptionModel {
  /// 处方ID
  String? prescriptionId;
  
  /// 患者ID
  String? patientId;
  
  /// 医生ID
  String? doctorId;
  
  /// 处方内容
  String? content;
  
  /// 处方状态
  int? status;
  
  /// 创建时间
  String? createTime;
  
  /// 更新时间
  String? updateTime;
  
  /// 药品列表
  List<MedicineModel>? medicines;
  
  /// 构造函数
  PrescriptionModel({
    this.prescriptionId,
    this.patientId,
    this.doctorId,
    this.content,
    this.status,
    this.createTime,
    this.updateTime,
    this.medicines,
  });
  
  /// 从JSON创建对象
  factory PrescriptionModel.fromJson(Map<String, dynamic> json) {
    return PrescriptionModel(
      prescriptionId: json['prescription_id']?.toString(),
      patientId: json['patient_id']?.toString(),
      doctorId: json['doctor_id']?.toString(),
      content: json['content']?.toString(),
      status: json['status'] is int ? json['status'] : int.tryParse(json['status']?.toString() ?? ''),
      createTime: json['create_time']?.toString(),
      updateTime: json['update_time']?.toString(),
      medicines: json['medicines'] != null 
          ? (json['medicines'] as List).map((item) => MedicineModel.fromJson(item)).toList()
          : null,
    );
  }
  
  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'prescription_id': prescriptionId,
      'patient_id': patientId,
      'doctor_id': doctorId,
      'content': content,
      'status': status,
      'create_time': createTime,
      'update_time': updateTime,
      'medicines': medicines?.map((item) => item.toJson()).toList(),
    };
  }
}

/// 药品信息数据模型
class MedicineModel {
  /// 药品ID
  String? medicineId;
  
  /// 药品名称
  String? name;
  
  /// 药品规格
  String? specification;
  
  /// 药品用量
  String? dosage;
  
  /// 药品用法
  String? usage;
  
  /// 药品价格
  double? price;
  
  /// 药品数量
  int? quantity;
  
  /// 构造函数
  MedicineModel({
    this.medicineId,
    this.name,
    this.specification,
    this.dosage,
    this.usage,
    this.price,
    this.quantity,
  });
  
  /// 从JSON创建对象
  factory MedicineModel.fromJson(Map<String, dynamic> json) {
    return MedicineModel(
      medicineId: json['medicine_id']?.toString(),
      name: json['name']?.toString(),
      specification: json['specification']?.toString(),
      dosage: json['dosage']?.toString(),
      usage: json['usage']?.toString(),
      price: json['price'] is double ? json['price'] : double.tryParse(json['price']?.toString() ?? ''),
      quantity: json['quantity'] is int ? json['quantity'] : int.tryParse(json['quantity']?.toString() ?? ''),
    );
  }
  
  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'medicine_id': medicineId,
      'name': name,
      'specification': specification,
      'dosage': dosage,
      'usage': usage,
      'price': price,
      'quantity': quantity,
    };
  }
}